USE [QLDL]
GO

/****** Object:  StoredProcedure [dbo].[sp_SuaThongTinDaiLy]    Script Date: 01/03/2011 15:27:41 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE procedure [dbo].[sp_SuaThongTinDaiLy]
	@MaDaiLy nvarchar(10), @TenDaiLy ntext, @MaLoaiDaiLy nvarchar(10), @MaQuan nvarchar(10), @DienThoai varchar(20), @DiaChi ntext, @Email varchar(50)
as
begin
	if exists(select * from DAILY where (TenDaiLy like @TenDaiLy) and (DiaChi like @DiaChi) and (MaLoaiDaiLy = @MaLoaiDaiLy) and (MaQuan = @MaQuan))
		return 1
	
	if (@MaQuan != (select MaQuan from DAILY where MaDaiLy = @MaDaiLy))
	begin	
		declare @SoDL int
		select @SoDL = COUNT(*) from DAILY where DAILY.MaQuan = @MaQuan
		if (@SoDL >= (select SoDLToiDa from QUAN where QUAN.MaQuan = @MaQuan))
			return 2
	end
		
	if ((LEN(@Email) > 0) and (@Email not like '_%@_%'))
		return 3

	update DAILY set TenDaiLy = @TenDaiLy,
					MaLoaiDaiLy = @MaLoaiDaiLy,
					MaQuan = @MaQuan,
					DienThoai = @DienThoai,
					DiaChi = @DiaChi,
					Email = @Email
				where MaDaiLy = @MaDaiLy
	return 0
end
GO


